﻿@model Wheeler.Web.ViewModels.UtilityModels.ListConfig
@using Wheeler.Web.Utility;
@using Wheeler.Web.ViewModels;
@using Wheeler.Common.Extensions;

@{
    TestResultSearchModel _SearchKeys = ViewData[AppConstants.ViewDataKeys.SEARCH_KEY] as TestResultSearchModel;
    String pageTitle = "Search Data Sheets";
    ViewBag.Title = Html.GetPageTitle(pageTitle);
    if(UserInfo.IsAdmin){
        ViewData[AppConstants.ViewDataKeys.SELECTED_MENU] = AdminMenu.EditTestResult;
    }
    else{
        ViewData[AppConstants.ViewDataKeys.SELECTED_MENU] = OperatorMenu.TestResults;
    }
    ViewData[AppConstants.ViewDataKeys.LIST_TYPE] = _SearchKeys.ViewType;
    long _TotalRecord = Model != null && Model.Pager != null ? Model.Pager.TotalRecord : 0;
}

<div class="jb-page-heading">
    <h1>@pageTitle</h1>
    <div>
        @if (_SearchKeys.HasKeys)
        {
            <text>List of matched Test Result(s) for your given search criteria.</text>
        }
        else {
            <text>Enter criteria in the following fields and click Search button to search test results.</text>
        }
    </div>
</div>


<div class="jb-page-content auto">
    @using (Html.BeginForm("Search", "TestResult", FormMethod.Post, new { id="frmSearch", @class="form-inline"}))
    {       
        <input id="RegistrationNo" name="RegistrationNo" type="text" maxlength="45" value="@_SearchKeys.RegistrationNo" placeholder="Enter Registration No" />
        <input id="FromDate" name="FromDate" type="text" maxlength="45" value="@_SearchKeys.FromDate" placeholder="Enter From Date" class="datepicker" />
        <input id="ToDate" name="ToDate" type="text" maxlength="45" value="@_SearchKeys.ToDate" placeholder="Enter To Date" class="datepicker" />
        <button class="btn btn-primary" type="submit" onclick="ShowLoadingAnimation(false);"><i class="icon-search icon-white"></i> Search</button>

        <div style="margin-top:4px;">
            <div>
                <div class="btn-group">                  
                    <button type="button" class="btn btn-small@(_SearchKeys.ViewType == "L" ? " active" : "" )" onclick="SetListType('L');"><i class="icon-th-list"></i> List View</button>
                    <button type="button" class="btn btn-small@(_SearchKeys.ViewType == "T" ? " active" : "" )" onclick="SetListType('T');"><i class="icon-th-large"></i> Tabular View</button>
                </div>
                @if (_TotalRecord > 0) {
                <button type="button" class="btn btn-small pull-right" onclick="ExportCSV();"><i class="icon-star"></i> Export</button>
                }
                <input type="hidden" id="ViewType" name="ViewType" value="@_SearchKeys.ViewType" />
            </div>
            @*<div class="span6">
                
            </div>*@
        </div>
    }
    <div class="clearfix"></div>
    @if (Model != null && Model.Pager != null)
    {
        //String autoClass = Model.Pager.TotalRecord > 0 ? "auto" : String.Empty;
        Model.Pager.UrlFormat = "/Private/TestResult/Search?RegistrationNo=" + 
            _SearchKeys.RegistrationNo.UrlEncode() + "&FromDate=" + _SearchKeys.FromDate.UrlEncode() + "&ToDate= " + _SearchKeys.ToDate.UrlEncode() + "&ViewType=" + _SearchKeys.ViewType + "&PageNo={0}";
        <div>
            @Html.Partial("_TestResultList", Model.DataSource)
            @Html.Partial("_Pager", Model.Pager)
        </div>
    }
</div>

@section PageHeadSection
{
    <link href="@Url.Content("~/Content/themes/jb-ui/jquery.ui.all.css")" rel="stylesheet" type="text/css" />    
}
@section JavaScriptPlaceHolder{
    @Html.JavaScript("jquery-ui-1.8.11.min.js", Url)
    @Html.Partial("_DatePicker")
    
    <script language="javascript" type="text/javascript">
        function SetListType(type) {
            ShowLoadingAnimation(false);
            $('#ViewType').val(type);
            $('#frmSearch').submit();
        }
        function ExportCSV() {
            var url = '/Private/TestResult/ExportCSV';
            var data = 'registrationNo=' + encodeURIComponent($('#RegistrationNo').val()) + '&fromDate=' + encodeURIComponent($('#FromDate').val()) + '&toDate=' + encodeURIComponent($('#ToDate').val());
            ShowLoadingAnimation(false);
            Ajax(url, data, function (fileName) {
                if (fileName.length > 0) {                    
                    window.location = '/Private/TestResult/DownloadCSV?File=' + fileName;
                    HideLoadingAnimation();
                }
            });
        }
    </script>
}

